package com.newhero.app.report.site.service.impl;

import java.lang.reflect.Field;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.newhero.app.report.persistence.entity.Disclose;
import com.newhero.app.report.persistence.entity.Post;
import com.newhero.app.report.persistence.entity.ProblemQuality;
import com.newhero.app.report.persistence.entity.ReportKind;
import com.newhero.app.report.persistence.entity.ReportRegion;
import com.newhero.app.report.persistence.mapper.DiscloseMapper;
import com.newhero.app.report.persistence.mapper.PostMapper;
import com.newhero.app.report.persistence.mapper.ProblemQualityMapper;
import com.newhero.app.report.persistence.mapper.ReportKindMapper;
import com.newhero.app.report.persistence.mapper.ReportRegionMapper;
import com.newhero.app.report.site.service.ISiteService;
import com.newhero.app.report.utils.DESFileUtil;
import com.newhero.app.report.utils.DesModleUtils;
import com.newhero.product.common.mybatis.mapper.EntityWrapper;
import com.newhero.product.common.service.impl.BaseServiceImpl;
import com.newhero.product.common.util.GTime;
import com.newhero.product.common.util.StringUtils;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zlf
 * @since 2018-09-07
 */
@Service
public class SiteServiceImpl extends BaseServiceImpl<DiscloseMapper, Disclose> implements ISiteService {

	@Autowired
	private ReportKindMapper reportKindMapper;
	@Autowired
	private PostMapper postMapper;
	@Autowired
	private ReportRegionMapper reportRegionMapper;
	@Autowired
	private ProblemQualityMapper problemQualityMapper;
	
	@Override
	public List<ReportKind> findReportKindList() {
		//信息种类
		EntityWrapper<ReportKind> reportKindWrapper = new EntityWrapper<ReportKind>();
		reportKindWrapper.orderBy("kind_no",true);
		List<ReportKind> reportKindList = reportKindMapper.selectList(reportKindWrapper);
		return reportKindList;
	}

	@Override
	public ReportKind getReportKindById(String id) {
		ReportKind reportKind = reportKindMapper.selectById(id);
		return reportKind;
	}

	@Override
	public List<Post> findPostList(String postKind) {
		
		EntityWrapper<Post> postWrapper = new EntityWrapper<Post>();
		postWrapper.eq("post_type",postKind);
		postWrapper.orderBy("post_no",true);
		List<Post> postList = postMapper.selectList(postWrapper);
		return postList;
	}

	@Override
	public Post getPostById(String id) {
		Post post = postMapper.selectById(id);
		return post;
	}

	@Override
	public ReportRegion getReportRegionById(String regionCode) {
		ReportRegion reportRegion = reportRegionMapper.selectById(regionCode);
		return reportRegion;
	}

	@Override
	public void saveDisclose(Disclose dis) {
		Disclose disclose = DiscloseEncryptAndDecrypt(dis,"0");
		this.insert(disclose);
		
	}
	
	public int getDisIpCount(String ip) throws Exception{
		
		int count = 0;
		EntityWrapper<Disclose> discloseWrapper = new EntityWrapper<Disclose>();
		//当前时间
		String ctime = GTime.getTime(GTime.YYYY_MM_DD);
		//当前日期加密
		ctime = DESFileUtil.encryptStr(ctime);
		//当前用户IP加密
		ip = DESFileUtil.encryptStr(ip);
		discloseWrapper.eq("dis_ip",ip);
		discloseWrapper.eq("dis_date",ctime);
		count = this.selectCount(discloseWrapper);
		
		return count;
	}
	
	public Disclose getDiscloseByReportCode(String reportCode){
		
		Disclose disclose = new Disclose();
		EntityWrapper<Disclose> discloseWrapper = new EntityWrapper<Disclose>();
		discloseWrapper.eq("report_code",reportCode);
		disclose = this.selectOne(discloseWrapper);
		return disclose;
	}
	
	public List<ProblemQuality> problemQualityList(){
		
		EntityWrapper<ProblemQuality> problemMapper = new EntityWrapper<ProblemQuality>();
		problemMapper.orderBy("problem_order",true);
		List<ProblemQuality> problemQualityList = problemQualityMapper.selectList(problemMapper);
		return problemQualityList;
	}
	
	/** 
     * 获取对象的所有属性值，对对象数据进行加密或者解密然后返回该对象 
	 * @throws SecurityException 
	 * @throws NoSuchFieldException 
	 * 加密状态desState:0加密 1解密
	 * 
     * */  
    private Disclose DiscloseEncryptAndDecrypt(Disclose o,String desState) {  
    	 
    	Disclose obj = new Disclose();
    	String[] fieldNames= DesModleUtils.getFiledName(o);  
	    Object[] value=new Object[fieldNames.length]; 
	     
	     for(int i=0;i<fieldNames.length;i++){  
	    	 if("serialVersionUID".equals(fieldNames[i])){
	    		 continue;
	    	 }
	         value[i] = DesModleUtils.getFieldValueByName(fieldNames[i], o); 
	         try {
				Field field = Disclose.class.getDeclaredField(fieldNames[i]);
				field.setAccessible(true);
				//获取当前属性的  类型
				String type = field.getType().toString();
				if("class java.lang.String".equals(type) && !"discloseId".equals(fieldNames[i]) && !"dateOrder".equals(fieldNames[i]) ){
					
					String str = "";
					if(StringUtils.isNotBlank((String) value[i])){
						if("0".equals(desState)){
							//加密对象值
							str = DESFileUtil.encryptStr((String) value[i]);
						}else{
							//解密对象值
							str = DESFileUtil.decryptStr((String) value[i]);
						}
						
					}
					field.set(obj, str);
				}else{
					field.set(obj, value[i]);
				}
				
			} catch (Exception e) {
				e.printStackTrace();
			}
	     }  

	     return obj;  
    } 
	
}
